package com.feizhi.common.reflect;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author shuaqiu
 *
 */
public final class ClassUtils {

    private ClassUtils() {
    }

    public static ParameterizedType getParameterizedType(final Class<?> target) {
        final Type[] types = getGenericType(target);
        if (types.length > 0 && types[0] instanceof ParameterizedType) {
            return (ParameterizedType) types[0];
        }
        return null;
    }

    public static Type[] getParameterizedTypes(final Class<?> target) {
        final Type[] types = getGenericType(target);
        if (types.length > 0 && types[0] instanceof ParameterizedType) {
            return ((ParameterizedType) types[0]).getActualTypeArguments();
        }
        return null;
    }

    public static Type[] getGenericType(final Class<?> target) {
        if (target == null) {
            return new Type[0];
        }
        final Type[] types = target.getGenericInterfaces();
        if (types.length > 0) {
            return types;
        }
        final Type type = target.getGenericSuperclass();
        if (type != null) {
            if (type instanceof ParameterizedType) {
                return new Type[] { type };
            }
        }
        return new Type[0];
    }

}
